package com.hjc.service.impl;

import com.hjc.entity.Purchase;
import com.hjc.entity.Result;
import com.hjc.mapper.PurchaseMapper;
import com.hjc.page.Page;
import com.hjc.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseServiceIpml implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Override
    public Result addPurchase(Purchase purchase) {
        int flag = purchaseMapper.insertPurchase(purchase);
        if (flag > 0) {
            return Result.ok("采购单添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "采购单添加失败！");
    }

    @Override
    public Page findPurchaseByPage(Purchase purchase, Page page) {
        int count = purchaseMapper.selectPurchaseCount(purchase);
        List<Purchase> purchaseList = purchaseMapper.selectPurchaseByPage(purchase, page);
        page.setTotalNum(count);
        page.setResultList(purchaseList);
        return page;
    }

    @Override
    public Result removePurchaseById(Integer buyId) {
        int flag = purchaseMapper.deletePurchaseById(buyId);
        if(flag > 0) {
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！");
    }

    @Override
    public Result setPurchaseById(Purchase purchase) {
        int flag = purchaseMapper.updatePurchaseById(purchase);
        if (flag > 0) {
            return Result.ok("更新成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "更新失败！");
    }
}
